package com.sch.wanandroid.ui.my.coin

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.entity.ArticleBean
import com.sch.wanandroid.entity.CoinRecordBean
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * Created by Sch.
 * Date: 2020/12/15
 * description:
 */
class CoinViewModel : ViewModel() {
    private val _coinRecordList = MutableLiveData<List<CoinRecordBean>>()
    val coinRecordListLiveData: LiveData<List<CoinRecordBean>> = _coinRecordList

    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg


    fun getCoinData(pageNum: Int) {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.getCoinRecordListData(pageNum)
                if (result.errorCode != 0) {
                    _errorMsg.value = result.errorMsg
                } else {
                    _coinRecordList.value = result.data.datas
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }
}